﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
using Ionic.Zip;
namespace RomEditor
{
    public partial class mainmenu : Form
    {
        plugins plg = new plugins();
        buildprop bp = new buildprop();
        public String path = Application.StartupPath;
        public mainmenu()
        {
            InitializeComponent();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            plg.Show();
        }

        private void mainmenu_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            status.Text = "آماده سازی...";
            if (persiankb.Checked) { System.IO.File.Copy(Path.Combine(path+ "\\payloads\\fakeyboardcm","LatinIME.apk"),Path.Combine(path + "\\temp\\system\\app","LatinIME.apk"), true); }
            if (setpath.Checked) { System.IO.File.Copy(Path.Combine(path + "\\payloads\\installer", "PackageInstaller.apk"), Path.Combine(path + "\\temp\\system\\app", "PackageInstaller.apk"), true); }
            status.Text = "ساختن رام برای نصب از CWM ...";
            using (ZipFile zip = new ZipFile())
            {
                zip.AddDirectory(path + "\\temp");
                zip.Save(path + "\\output\\ROM"+DateTime.Now.Millisecond.ToString()+".zip");
            }
            status.Text = "انجام شد.";
        }

        private void loadrom_Click(object sender, EventArgs e)
        {
            status.Text = "لطفا کمی صبر کنید....";
           // status.Refresh();
            DeleteDirectory(path + "/temp");
            Directory.CreateDirectory(path + "/temp");
            using (ZipFile zip = new ZipFile(rompath.Text))
            {
                foreach (ZipEntry f in zip)
                {
                    status.Text = "استخراج :‌ "+f.FileName;
                    //status.Refresh();
                    f.Extract(path + "/temp");
                }
            }
            System.IO.StreamReader myFile = new System.IO.StreamReader(path + "/temp/system/build.prop");
            string bptext = myFile.ReadToEnd();
            myFile.Close();
            bp.setbuildprobtext(bptext);
            status.Text = "انجام شد...";
        }

        private void bpbtn_Click(object sender, EventArgs e)
        {
            bp.Show();
        }


        public static void DeleteDirectory(string target_dir)
        {
            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);

            foreach (string file in files)
            {
                try
                {

                    File.SetAttributes(file, FileAttributes.Normal);
                    File.Delete(file);
                    
                }
                catch (IOException)
                {
                    
                }


            }

            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }

            Directory.Delete(target_dir, true);
        }

        private void status_Click(object sender, EventArgs e)
        {

        }

        private void status_TextChanged(object sender, EventArgs e)
        {
            status.Refresh();
        }

        private void selectrom_Click(object sender, EventArgs e)
        {

	    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            String file = openFileDialog1.FileName;
            rompath.Text = file;
        }
        }
    





    }
}
